import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;
import java.awt.event.ActionEvent;

public class Main extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTable table;
	private JScrollPane scrollPane;
	private Vector<String> columnNames = new Vector<String>();
	private Vector<Vector> rowData = new Vector<Vector>();

	private DefaultTableModel defaultTableModel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {

		columnNames.add("id");
		columnNames.add("姓名");
		columnNames.add("年龄");
		columnNames.add("班级");
		columnNames.add("其他");
		ManageMysqlDB mmdb = new ManageMysqlDB();
		String sql = "select * from student";
		mmdb.connectionDB("stu_user", "88888888");
		mmdb.queryDB(sql);
		try {
			rowData.clear();
			while (mmdb.rs.next()) {
				Vector row = new Vector();// 设置每一行的值
				row.add(mmdb.rs.getString("sno"));
				row.add(mmdb.rs.getString("sname"));
				row.add(mmdb.rs.getString("age"));
				row.add(mmdb.rs.getString("sex"));
				row.add(mmdb.rs.getString("dept"));
				rowData.add(row);
			} // System.out.println(mmdb.rs.getString("sno")+"\t"+mmdb.rs.getString("sname")+"\t"+mmdb.rs.
				// getString("age")+"\t"+mmdb.rs.getString("sex")+"\t"+mmdb.rs.getString("dept"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mmdb.closeDB();
		defaultTableModel = new DefaultTableModel(rowData, columnNames);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);

		JLabel lblNewLabel = new JLabel("学号");
		lblNewLabel.setBounds(50, 44, 61, 16);
		getContentPane().add(lblNewLabel);

		textField_1 = new JTextField();
		textField_1.setBounds(140, 39, 130, 26);
		getContentPane().add(textField_1);
		textField_1.setColumns(10);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String sno = textField_1.getText();
				ManageMysqlDB mmdb = new ManageMysqlDB();
				String sql = "select * from student where sno='" + sno + "'";
				mmdb.connectionDB("stu_user", "88888888");
				mmdb.queryDB(sql);
				try {
					rowData.clear();
					while (mmdb.rs.next()) {
						Vector row = new Vector();// 设置每一行的值
						row.add(mmdb.rs.getString("sno"));
						row.add(mmdb.rs.getString("sname"));
						row.add(mmdb.rs.getString("age"));
						row.add(mmdb.rs.getString("sex"));
						row.add(mmdb.rs.getString("dept"));
						rowData.add(row);
						// System.out.println(mmdb.rs.getString("sno")+"\t"+mmdb.rs.getString("sname")+"\t"+mmdb.rs.
						// getString("age")+"\t"+mmdb.rs.getString("sex")+"\t"+mmdb.rs.getString("dept"));
					}
					
					//defaultTableModel.getDataVector().clear();
					defaultTableModel.setDataVector(rowData, columnNames);
					System.out.println(rowData);
					table.setModel(defaultTableModel);
					table.updateUI();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mmdb.closeDB();

			}
		});
		btnNewButton.setBounds(288, 39, 117, 29);
		getContentPane().add(btnNewButton);

		table = new JTable(defaultTableModel);
//		table.setBounds(67, 214, 243, -94);
		scrollPane = new JScrollPane(table);
		scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
		scrollPane.setBounds(33, 77, 400, 200);
		getContentPane().add(scrollPane);
	}
}
